import {defineStore} from "pinia"
import {
    reactive,
    ref,
    readonly
} from "vue"
import { getDormitory, getFloor, getRoomNo, getBednumber } from "@/api/address.js";

export const useCheckInStore = defineStore("checkIn",() => {
    const checkInInfo = reactive({
        dormitory: "", // 选中的宿舍
        floor:"", // 选中的楼层
        room:"", // 选中的房间
        bednumber:"" // 选中的床号
    });
    const readCheckInfo = readonly(checkInInfo);
    // 获取宿舍信息
    const getDormitoryInfo = async (classId) => {
        const {data} = await getDormitory({classId})
        if(data.length === 1){
            setDormitory(data[0])
        }
        return data;
    }
    // 获取楼层信息
    const getFloorInfo = async ({dormitoryId,floorId,classId}) => {
        const {data} = await getFloor({dormitoryId,floorId,classId})
        if(data.length === 1){
            setFloor(data[0])
        }
        return data
    }
    // 获取房间号信息
    const getRoomNoInfo = async ({dormitoryId,floorId,roomNoId,classId}) => {
        const {data} = await getRoomNo({dormitoryId,floorId,roomNoId,classId});
        return data
    }
    // 获取床号信息
    const getBednumberInfo = async ({roomNoId,bednumberId})=> {
        const {data} = await getBednumber({roomNoId,bednumberId});
        return data
    }
    // 赋值选中的宿舍
    const setDormitory = (dormitory) =>{
        checkInInfo.floor = ""
        checkInInfo.room = ""
        checkInInfo.bednumber = ""
        checkInInfo.dormitory = dormitory
    }
    // 赋值选中的楼层
    const setFloor = (floor) => {
        checkInInfo.room = ""
        checkInInfo.bednumber = ""
        checkInInfo.floor = floor
    }
    // 赋值选中的房间
    const setRoom = (room) => {
        checkInInfo.bednumber = ""
        checkInInfo.room = room
    }
    // 赋值选中的床号
    const setBednumber = (bednumber) => {
        checkInInfo.bednumber = bednumber
    }
    return{
        readCheckInfo,
        getDormitoryInfo,
        getFloorInfo,
        getRoomNoInfo,
        getBednumberInfo,
        setDormitory,
        setFloor,
        setRoom,
        setBednumber
    }
    
})